package com.and.dao.impl;

import java.util.List;

import com.and.dao.PageDao;

/**
 * @Title: PageDaoImpl.java
 * @Package com.and.dao.impl
 * @Description: TODO(添加描述)
 * @author hanchunping
 * @date 2010-7-8 上午11:26:05
 * @version V1.0
 */
public class PageDaoImpl implements PageDao {

	private List<?> list;
	// 默认每页显示10条
	private int perPage = 10;
	// 默认当前页为 第一页
	private int currentPage = 1;
	// 默认页数 为 一页
	private int pageNum = 1;
	// 当前List
	private List<?> cList;
	//
	private int allNum;

	/**
	 * 默认构造函数
	 * 
	 * @Title:
	 * @Description:
	 */
	public PageDaoImpl() {

	}

	/**
	 * 带参数的构造函数
	 * 
	 * @Title:
	 * @Description:
	 * @param list
	 *            数据集
	 * @param perPage
	 *            每页显示条数
	 */
	public PageDaoImpl(List<?> list, int perPage) {
		this.list = list;
		if (perPage >= 1) {
			this.perPage = perPage;
		}
		if (list != null && list.size() > this.perPage) {
			this.cList = list.subList(0, this.perPage - 1);
		}
		// 页数
		this.allNum = this.list.size();
		if (this.allNum % this.perPage == 0) {
			this.pageNum = this.allNum / this.perPage;
		} else {
			this.pageNum = this.allNum / this.perPage + 1;
		}
	}

	/**
	 * 
	 * @Title: getCount
	 * @Description: 记录总数
	 * @return 设定文件
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.allNum;
	}

	/**
	 * 
	 * @Title: getCurrentPage
	 * @Description: 当前页数
	 * @return 设定文件
	 */
	@Override
	public int getCurrentPage() {
		// TODO Auto-generated method stub
		return this.currentPage;
	}

	/**
	 * 
	 * @Title: getPageNum
	 * @Description: 获取总页数
	 * @return 设定文件
	 */
	@Override
	public int getPageNum() {
		// TODO Auto-generated method stub

		return this.pageNum;
	}

	/**
	 * 
	 * @Title: getPerPage
	 * @Description: get 每页显示的条数
	 */
	@Override
	public int getPerPage() {
		// TODO Auto-generated method stub
		return this.perPage;
	}

	/**
	 * 
	 * @Title: gotoPage
	 * @Description: go to x page
	 * @param n
	 *            设定文件
	 */
	@Override
	public void gotoPage(int n) {
		// TODO Auto-generated method stub
		if (n > this.pageNum) {
			n = this.pageNum;
			this.currentPage = n;
		} else {
			this.currentPage = n;
		}
	}

	/**
	 * 
	 * @Title: hasNextPage
	 * @Description: 是否有下一页
	 * @return 设定文件
	 */
	@Override
	public boolean hasNextPage() {
		// TODO Auto-generated method stub
		this.currentPage++;
		if (this.currentPage <= this.pageNum) {
			return true;
		} else {
			this.currentPage = this.pageNum;
		}
		return false;
	}

	/**
	 * 
	 * @Title: hasHeadpage
	 * @Description:是否有上一页
	 * @return 设定文件
	 */
	@Override
	public boolean hasPrepage() {
		// TODO Auto-generated method stub
		this.currentPage--;
		if (this.currentPage > 0) {
			return true;
		} else {
			this.currentPage = 1;
		}
		return false;
	}

	/**
	 * 
	 * @Title: headPage
	 * @Description: 首页
	 */
	@Override
	public void headPage() {
		// TODO Auto-generated method stub
		this.currentPage = 1;
	}

	/**
	 * 
	 * @Title: lastPage
	 * @Description: 最后一页
	 */
	@Override
	public void lastPage() {
		// TODO Auto-generated method stub
		this.currentPage = this.pageNum;

	}

	/**
	 * 
	 * @Title: nextPage
	 * @Description: 下一页
	 */
	@Override
	public void nextPage() {
		// TODO Auto-generated method stub
		this.hasNextPage();
	}

	/**
	 * 
	 * @Title: prePage
	 * @Description: 前一页
	 */
	@Override
	public void prePage() {
		// TODO Auto-generated method stub
		this.hasPrepage();
	}

	/**
	 * 
	 * @Title: setPerPage
	 * @Description: 每页显示的条数
	 * @param page
	 *            设定文件
	 */
	@Override
	public void setPerPage(int page) {
		// TODO Auto-generated method stub
		this.perPage = page;
	}

	/**
	 * 
	 * @Title: currentList
	 * @Description: 当前记录集
	 * @return 设定文件
	 */
	@Override
	public List<?> getCurrentList() {

		if (this.currentPage == this.pageNum) {
			this.cList = this.list.subList((this.currentPage-1) * this.perPage,
					this.allNum);
		} else {

			this.cList = this.list.subList((this.currentPage - 1)
					* this.perPage, this.currentPage * (this.perPage));
		}
		return this.cList;
	}

	/**
	 * 
	 * @Title: initList
	 * @Description: 初始化 list
	 * @param list
	 *            设定文件
	 */
	public void initList(List<?> list) {

		this.list = list;
		if (this.perPage <= 0) {
			this.perPage = 10;
		}
		/*
		 * if (this.list != null && this.list.size() > this.perPage) {
		 * this.cList = this.list.subList(0, this.perPage - 1); }
		 */
		// 页数
		this.allNum = this.list.size();
		if (this.allNum % this.perPage == 0) {
			this.pageNum = this.allNum / this.perPage;
		} else {
			this.pageNum = this.allNum / this.perPage + 1;
		}
	}
}
